package com.cskaoyan.order.biz.handler;

import com.cskaoyan.constants.UserRetCode;
import com.cskaoyan.mall.commons.exception.ValidateException;
import com.cskaoyan.mall.dto.ItemToOrderDTO;
import com.cskaoyan.order.biz.context.CreateOrderContext;
import com.cskaoyan.order.biz.context.TransHandlerContext;
import com.cskaoyan.order.constant.OrderRetCode;
import com.cskaoyan.order.dal.entitys.Stock;
import com.cskaoyan.order.dal.persistence.StockMapper;
import com.cskaoyan.order.dto.CartProductDto;
import com.cskaoyan.order.remote.ShoppingFeignClient;
import com.cskaoyan.order.remote.UserFeignClient;
import com.cskaoyan.user.UserVerifyDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.util.Iterator;

@Slf4j
@Component
public class ValidateHandler extends AbstractTransHandler {

    @Autowired
    StockMapper stockMapper;
    @Autowired
    ShoppingFeignClient shoppingFeignClient;
    @Autowired
    UserFeignClient userFeignClient;

    @Override
    public boolean isAsync() {
        return false;
    }

    /**
     * @param context
     * @return boolean
     * @description 检验参数
     * @author silic xia
     * @since 2022/12/03 10:27
     */
    @Override
    public boolean handle(TransHandlerContext context) {
        // 用户状态是否正常
        CreateOrderContext orderContext = (CreateOrderContext) context;
        UserVerifyDTO userVerify = userFeignClient.selectUserVerifyByPrimaryKey(orderContext.getUserId());

        ((CreateOrderContext) context).setUserName(userVerify.getUsername());

        if ("N".equals(userVerify.getIsExpire()) || "N".equals(userVerify.getIsVerify())) {
            throw new ValidateException(UserRetCode.USER_INFOR_INVALID.getCode(), UserRetCode.USER_INFOR_INVALID.getMessage());
        }
        // 商品有效性检查
        Iterator<CartProductDto> iterator = ((CreateOrderContext) context).getCartProductDtoList().iterator();
        if (iterator.hasNext()) {
            CartProductDto product = iterator.next();
            // 商品是否上架
            @NotNull @Min(0) Long productId = product.getProductId();
            ItemToOrderDTO item = shoppingFeignClient.selectItemByPrimaryKey(productId);
            if (item.getStatus() != 1){
                throw new ValidateException(OrderRetCode.ITEM_SOLD_OUT.getCode(), OrderRetCode.ITEM_SOLD_OUT.getMessage());
            }
            // 商品库存是否大于0
            Stock stock = null;
            try {
                stock = stockMapper.selectStock(product.getProductId());
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("stockMapper异常捕获：SQL异常！数据库异常！");
            }
            if (stock.getStockCount() <= 0){
                throw new ValidateException(OrderRetCode.PART_OF_PRODUCTS_STOCK_NOT_INIT.getCode(), OrderRetCode.PART_OF_PRODUCTS_STOCK_NOT_INIT.getMessage());
            }
        }
        return true;
    }
}
